<?php

use Psr\Container\ContainerInterface;
use function Hyperf\Config\config;
use Hyperf\Context\ApplicationContext;
use Hyperf\HttpServer\Contract\ResponseInterface;
use App\Utils\Dump;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use Hyperf\Redis\RedisFactory;


if (!function_exists('convert_size')) {
    /**
     * 将字节转化为 kb mb 等单位
     * @param $size
     * @return string
     */
    function convert_size($size)
    {
        $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
        return @round($size / pow(1024, $i = floor(log($size, 1024))), 2) . ' ' . $unit[$i];
    }
}

if (!function_exists('isProd')) {
    /**
     * Notes: 判断是否生产环境 关闭日志 去掉调试参数等等
     * User: Tom
     * DateTime: 2021/7/27 15:07
     * @return bool
     */
    function isProd(): bool
    {
        return config('app_env') === 'production';
    }
}

if (!function_exists('isDevelopment')) {
    /**
     * Notes: 判断是否测试环境
     * User: Ken
     * DateTime: 2022/12/15 15:36
     * @return bool
     */
    function isDevelopment(): bool
    {
        return config('app_env') === 'development';
    }
}

if (!function_exists('isLocal')) {
    /**
     * Notes: 判断是否是本地环境 关闭日志 去掉调试参数等等
     * User: Tom
     * DateTime: 2021/7/27 15:07
     * @return bool
     */
    function isLocal(): bool
    {
        return config('app_env') === 'local';
    }
}

if (!function_exists('container')) {
    /**
     * Notes: 获取 ApplicationContext
     * User: ken
     * DateTime: 2021/7/27 15:07
     * @return ContainerInterface
     */
    function container(): ContainerInterface
    {
        return ApplicationContext::getContainer();
    }
}

if (!function_exists('response')) {
    /**
     * Notes: 获取Response
     * User: ken
     * DateTime: 2021/7/27 15:07
     * @return ResponseInterface
     */
    function response(): ResponseInterface
    {
        return container()->get(ResponseInterface::class);
    }
}


if (!function_exists('dump')) {
    /**
     * Notes: 打印测试数据
     * User: ken
     * DateTime: 2024/10/16
     * @return mixed
     */
    function dump(...$var): Dump
    {
        $dump = container()->get(Dump::class);
        if (!empty($var)) {
            $dump->info(...$var);
        }
        return $dump;
    }
}

if (!function_exists('abort')) {
    /**
     * Notes: 抛出BusinessException异常
     * User: caijw
     * DateTime: 2021/9/18 10:10
     * @param int $code
     * @param array $param
     * @throws BusinessException
     */
    function abort(int $code, array $param = [])
    {
        if ($code == ErrorCode::SUCCESS) {
            return;
        }
        throw new BusinessException($code, $param);
    }
}

if (!function_exists('redis')) {
    /**
     * Notes: 获取REDIS实例
     * User: caijw
     * DateTime: 2021/9/18 10:10
     * @param string $db_name
     */
    function redis($db_name = 'default')
    {
        return container()->get(RedisFactory::class)->get($db_name);
    }
}

/**
 * 判读字符串是否为json
 */
if (!function_exists('isJson')) {
    /**
     * Finds an entry of the container by its identifier and returns it.
     * @param string $string
     */
    function isJson($string)
    {
        json_decode($string);
        return (json_last_error() == JSON_ERROR_NONE);
    }
}